<?php
//任务处理动作后台
// header("content-type:text/html;charset=utf-8");
include_once("conn.php");
//设置默认时区
date_default_timezone_set('PRC');
$now_date = date('Y-m-d H:i:s', time());

if (empty($_POST['action'])) {
    echo "<script>alert('非法传参！');window.location.href='../a-main.php';</script>";
    die();
}

$action = $_POST['action'];
//任务派发
if ($action == 'allocated') {
    $fix_id = $_POST['fix_id'];
    $r_id = $_POST['fix_repairer'];
    $sql_allocated = "INSERT INTO tb_fix_repairer SET fix_id='" . $fix_id . "', r_id='" . $r_id . "', r_date='" . $now_date . "'";
    $sql_update = "UPDATE tb_fixinfo_request SET fix_status='1' WHERE fix_id='" . $fix_id . "'";

    // 开始事务
    $conn_DB->begin_transaction();
    if ($conn_DB->query($sql_allocated) && $conn_DB->query($sql_update)) {
        $conn_DB->commit();
        echo "<script>alert('任务分配成功！');window.location.href='../a-tasks-pending.php';</script>";
    } else {
        // 执行不成功回滚
        $conn_DB->rollback();
        echo "<script>alert('任务分配失败！请重试！');history.go(-1);</script>";
    }
}
//添加维修员
if ($action == 'add_repairer') {
    $r_id = $_POST['r_id'];
    $r_name = $_POST['r_name'];
    $r_phone = $_POST['r_phone'];
    $r_sex = $_POST['r_sex'];
    $sql_add = "INSERT INTO tb_repairer SET r_id='" . $r_id . "', r_name='" . $r_name . "', r_phone='" . $r_phone . "', r_sex='" . $r_sex . "'";

    if (mysqli_query($conn_DB, $sql_add)) {
        echo "<script>alert('添加成功！');window.location.href='../a-repairer.php';</script>";
    } else {
        echo "<script>alert('添加失败！请重试！');history.go(-1);</script>";
    }
}

//停用维修员账号
if ($action == 'del_repairer') {
    $r_id = $_POST['r_id'];
    $sql_select = "SELECT COUNT(*) FROM tb_fixinfo_request
        INNER JOIN tb_fix_repairer ON tb_fix_repairer.fix_id = tb_fixinfo_request.fix_id
        AND tb_fixinfo_request.fix_status = '1'
        AND tb_fix_repairer.r_id ='" . $r_id . "'";
    $result = mysqli_query($conn_DB, $sql_select);
    $row = mysqli_fetch_array($result);
    if ($row[0] > 0) {
        echo "<script>alert('该维修员正在处理任务，无法操作！');history.go(-1);</script>";
    } else {
        $sql_del = "UPDATE tb_repairer SET r_status = '0' WHERE r_id='" . $r_id . "'";
        if (mysqli_query($conn_DB, $sql_del)) {
            echo "<script>alert('操作成功！');window.location.href='../a-repairer.php';</script>";
        } else {
            echo "<script>alert('操作失败！请重试！');history.go(-1);</script>";
        }
    }
}

//启用维修员账号
if ($action == 'recover_repairer') {
    $r_id = $_POST['r_id'];
    $sql_del = "UPDATE tb_repairer SET r_status = '1' WHERE r_id='" . $r_id . "'";
    if (mysqli_query($conn_DB, $sql_del)) {
        echo "<script>alert('启用成功！');window.location.href='../a-repairer.php';</script>";
    } else {
        echo "<script>alert('启用失败！请重试！');history.go(-1);</script>";
    }
}

//修改维修员信息
if ($action == 'change_repairer') {
    $r_id = $_POST['r_id'];
    $r_name = $_POST['r_name'];
    $r_phone = $_POST['r_phone'];
    $r_sex = $_POST['r_sex'];
    $r_status = $_POST['r_status'];
    $sql_change = "UPDATE tb_repairer SET r_name='" . $r_name . "', r_phone='" . $r_phone . "', r_sex='" . $r_sex . "', r_status='" . $r_status . "' WHERE r_id='" . $r_id . "'";
    if (mysqli_query($conn_DB, $sql_change)) {
        echo "<script>alert('修改成功！');window.location.href='../a-repairer.php';</script>";
    } else {
        echo "<script>alert('修改失败！请重试！');history.go(-1);</script>";
    }
}

//修改维修故障类型信息
if ($action == 'leave_change') {
    $leave_id = $_POST['leave_id'];
    $leave_name = $_POST['leave_name'];
    $leave_num = $_POST['leave_num'];

    $sql_select = "SELECT * FROM tb_fix_leave WHERE leave_name='" . $leave_name . "'";
    $result = mysqli_query($conn_DB, $sql_select);
    if (mysqli_num_rows($result) > 0) {
    //     echo "<script>alert('该类型名称已存在！不可操作！');history.go(-1);</script>";
    // } else {
        $sql_change = "UPDATE tb_fix_leave SET leave_name='" . $leave_name . "', leave_num='" . $leave_num . "', update_time ='" . $now_date . "' WHERE id='" . $leave_id . "'";
        if (mysqli_query($conn_DB, $sql_change)) {
            echo "<script>alert('修改成功！');window.location.href='../a-type.php';</script>";
        } else {
            echo "<script>alert('修改失败！请重试！');history.go(-1);</script>";
        }
    }

}

//新增维修故障类型信息
if ($action == 'leave_add') {
    $leave_name = $_POST['leave_name'];
    $leave_num = $_POST['leave_num'];
    $sql_select = "SELECT * FROM tb_fix_leave WHERE leave_name='" . $leave_name . "'";
    $result = mysqli_query($conn_DB, $sql_select);
    if (mysqli_num_rows($result) > 0) {
        echo "<script>alert('该类型名称已存在！不可操作！');history.go(-1);</script>";
    } else {
        $sql_add = "INSERT INTO tb_fix_leave SET leave_name='" . $leave_name . "', leave_num='" . $leave_num . "', update_time ='" . $now_date . "'";
        if (mysqli_query($conn_DB, $sql_add)) {
            echo "<script>alert('添加成功！');window.location.href='../a-type-add.php';</script>";
        } else {
            echo "<script>alert('添加失败！请重试！');history.go(-1);</script>";
        }
    }
}